import datetime
import xmltodict
from config import db


def parse(result):
    save_list = []
    for res in result:
        print(res["name"] + " start parse...")
        doc = xmltodict.parse(res["data"])
        items = doc["rss"]["channel"]["item"]
        for item in items:
            pub_date = ''
            link = ''
            author = ''
            description = ''
            if 'pubDate' in item.keys():
                pub_date = item["pubDate"]
            else:
                pub_date = datetime.datetime.now().strftime('%Y-%m-%d')

            if 'link' in item.keys():
                link = item["link"]

            if 'author' in item.keys():
                t = item["author"]
                t.replace("'", "")
                author = r"%s" % t

            if 'description' in item.keys():
                t = item["description"]
                t.replace("'", "")
                description = r"%s" % t

            t = item["title"]
            t.replace("'", "")
            s = item["guid"]['#text']
            s.replace("'", "")
            article = {
                "title": r"%s" % t,
                "description": description,
                "pubDate": pub_date,
                "link": link,
                "author": author,
                "guid": s,
                "station": res["name"]
            }
            save_list.append(article)
        print(res["name"] + " parse finish")
    save_sql(save_list)


def save_sql(save_list):
    cursor = db.cursor()
    for item in save_list:
        print(item["station"] + "Cache Start...")
        try:
            sql = "select * from Article where guid = '%s'" % (item["guid"])
            cursor.execute(sql)
            results = cursor.fetchall()
            if len(results) == 0:
                sql_save = "insert into Article(guid, title, description, link, author, pubDate, station) values ('%s', '%s', '%s', '%s', '%s', '%s', '%s')" \
                            % (
                               item["guid"],
                               item["title"],
                               item["description"],
                               item["link"],
                               item["author"],
                               item["pubDate"],
                               item["station"]
                           )
                cursor.execute(sql_save)
                db.commit()
        except Exception:
            print("sql has error!")
            db.rollback()
        print(item["station"] + "Cache Finish")
    db.close()
